package com.example.demo.controller;

import com.example.demo.annotation.RedisLock;
import com.example.demo.service.ISayHelloService;
import com.example.demo.service.impl.DataSourceService;
import com.example.demo.service.impl.SayHelloServiceImpl1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * @author MarioZzz
 * @create 2023-08-02 18:23
 */
@RestController()
@RequestMapping("/test")
@Slf4j
@CrossOrigin(origins = "*", methods = {org.springframework.web.bind.annotation.RequestMethod.GET, org.springframework.web.bind.annotation.RequestMethod.POST})
public class TestController {

    @Resource
    private DataSourceService dataSourceService;

    @GetMapping("/getUser")
    public Map getUser() {
        Map res = new HashMap();
        res.put("data", "MarioZzz");
        res.put("success", true);
        System.out.println(res);
        return res;
    }

    @GetMapping("/getUrl")
    public String getUrl() {
//        Map<String, String> res = new HashMap<>(4);
//        Map<String, String> map = new HashMap<>(4);
//        map.put("url", "http://172.19.132.25:8080/swagger-ui.html");
//        res.put("data", map.toString());
//        String jsonString = JSON.toJSONString(res);
//        System.out.println(jsonString);
        String res = "{\"success\":true,\"data\":{\"redirectUrl\":\"http://172.16.157.219/idp/authCenter/authenticate?response_type=code&state=3d0d62d3b78f48b292c52e0d1de9b26f&redirect_uri=http://172.16.224.96:16786/portal/auth-center/api/iamAuth/getUserInfo&client_id=dcsc\"}}";
        System.out.println(res);
        return res;
    }

    @GetMapping("/getAll")
    public Map<String, Object> getAllDsInfo() throws SQLException {
        Map<String, Object> resMap;
        String jsonString = "{\n" +
                "        \"type\": \"POSTGRES\",\n" +
                "        \"url\": \"jdbc:postgresql://10.92.190.20:7300/csgbi\",\n" +
                "        \"password\": \"sjzlgj#2022\",\n" +
                "        \"driver\": \"org.postgresql.Driver\",\n" +
                "        \"username\": \"sjzlgl\"\n" +
                "    }";
        String type = "POSTGRES";
        resMap = dataSourceService.pullDataSource(jsonString, type);
        return resMap;
    }

    @GetMapping("/getAllTest")
    public Map<String, Object> getAllDsInfoTest() throws SQLException {
        Map<String, Object> resMap;
        String jsonString = "{\n" +
                "        \"type\": \"POSTGRES\",\n" +
                "        \"url\": \"jdbc:postgresql://10.92.190.20:7300/csgbi\",\n" +
                "        \"password\": \"sjzlgj#2022\",\n" +
                "        \"driver\": \"org.postgresql.Driver\",\n" +
                "        \"username\": \"sjzlgl\"\n" +
                "    }";
        String type = "POSTGRES";
        resMap = dataSourceService.pullDataSource(jsonString, type);
        return resMap;
    }

    @GetMapping("hello")
    @RedisLock
    public String sayHello() throws InterruptedException {
        log.info("打印一条日志{}", LocalDate.now());

        Thread.sleep(80000);
        return "Hello";
    }

    @Resource
    private ISayHelloService sayHelloService;

    @GetMapping("interfaceImplTest")
    public String interfaceImplTest() {
        sayHelloService.sayHello();
        return "success";
    }

    @GetMapping("interfaceImplTest1")
    public String interfaceImplTest1() {
        SayHelloServiceImpl1 sayHelloServiceImpl1 = new SayHelloServiceImpl1();
        sayHelloServiceImpl1.sayHello();
        return "success";
    }
}
